"""
Case Type   : 24年兼容性问题单分析-创建表指定字符集不支持时，继承数据库默认字符集
Case Name   : 测试创建表指定字符集不支持时，继承数据库默认字符集，列指定不支持得字符集，继承表的字符集
Create At   : 2024-11-26
Owner       : lvlintao
Description :
    1、连接B库设置相关配置，创建指定字符集的数据库
    2、创建未支持字符集的表，检查是否支持继承数据库的字符集;
    3、修改表中列为不支持的字符集，检查是否继承表的字符集;
    4、清理环境
Expect      :
    1、建库成功
    2、继承数据库字符集
    3、继承表的字符集
    4、清理环境成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

logger = Logger()


class BugFixTestCase(unittest.TestCase):
    def setUp(self):
        logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.comsh = CommonSH('PrimaryDbBmode')
        self.c_db = f"set b_format_behavior_compat_options=default_collation,enable_multi_charset;" \
                    f"drop schema if exists test_db_case003 cascade;" \
                    f"create schema test_db_case003 charset='utf8' collate='utf8_general_ci';"
        self.c_tb = f"set b_format_behavior_compat_options=default_collation,enable_multi_charset;" \
                    f"set current_schema=test_db_case003;" \
                    f"create table test_case003(id int,name varchar(100)) charset=utfmb4 collate=utfmb4_0900_ai_ci;"
        self.show_tb = f"set current_schema=test_db_case003;" \
                       f"show create table test_case003;"
        self.show_col = f"set b_format_behavior_compat_options=default_collation;" \
                        f"set current_schema=test_db_case003;" \
                        f"\d+ test_case003"
        self.alter_tb = f"set b_format_behavior_compat_options=default_collation,enable_multi_charset;" \
                        f"set current_schema=test_db_case003;" \
                        f"alter table test_case003 modify name varchar(10) charset 'utf8mb4' collate 'uft8mb4_0900_ai_ci';"
        self.clean_tb = "drop table test_db_case003.test_case003 cascade;"
        self.clean_db = "drop schema test_db_case003 cascade;"

    def test_bugfix(self):
        text = '--step1:连接B库设置相关配置，创建指定字符集的数据库;expect:建库成功--'
        logger.info(text)
        c_result = self.comsh.execut_db_sql(self.c_db)
        logger.info(c_result)
        self.assertIn(self.constant.CREATE_SCHEMA_SUCCESS_MSG, c_result, '执行失败' + text)

        text = '--step2:创建未支持字符集的表，检查是否支持继承数据库的字符集;expect:继承数据库字符集--'
        logger.info(text)
        c_result = self.comsh.execut_db_sql(self.c_tb)
        logger.info(c_result)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, c_result,
                      '执行失败' + text)
        show_result = self.comsh.execut_db_sql(self.show_tb)
        logger.info(show_result)
        self.assertIn('CHARACTER SET = "" COLLATE = "default"', show_result, '执行失败' + text)

        text = '--step3:修改表中列为不支持的字符集，检查是否继承表的字符集;expect:继承表的字符集--'
        logger.info(text)
        al_result = self.comsh.execut_db_sql(self.alter_tb)
        logger.info(al_result)
        self.assertIn(self.constant.ALTER_TABLE_MSG, al_result,
                      '执行失败' + text)
        show_result = self.comsh.execut_db_sql(self.show_tb)
        logger.info(show_result)
        show_result = self.comsh.execut_db_sql(self.show_col)
        logger.info(show_result)
        self.assertIn("collate=100", show_result, '执行失败' + text)

    def tearDown(self):
        text = '--step4:清理环境，恢复默认值;expect:成功--'
        logger.info(text)
        clean_res = self.comsh.execut_db_sql(self.clean_tb)
        logger.info(clean_res)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, clean_res,
                      '执行失败' + text)
        clean_res = self.comsh.execut_db_sql(self.clean_db)
        logger.info(clean_res)
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG, clean_res,
                      '执行失败' + text)
        logger.info(f'-----{os.path.basename(__file__)} end-----')
